package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Tcomment;
import com.xy.manage.service.TcommentService;



@Controller
public class TcommentAction{
	private static final Logger log = LoggerFactory.getLogger(TcommentAction.class);
	public TcommentAction() {
	}
 
 
 	/**
	 * 民意警务意见采集新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcommentAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tcomment", new Tcomment());
        setDict(model);
		return "tcommentEdit";
	}

	/**
	 * 民意警务意见采集新增保存
	 * 
	 * @param tcomment
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tcommentSave.do")
	public String save(Tcomment tcomment, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tcomment, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tcomment = tcommentService.save(tcomment);
		return "redirect:v_tcommentList.do";
	}

	/**
	 * 民意警务意见采集删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tcommentDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tcommentService.remove(id);
			}
		}
		return "redirect:v_tcommentList.do";
	}

	/**
	 * 民意警务意见采集编辑页面
	 * 
	 * @param pid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcommentEdit.do")
	public String edit(long pid, HttpServletRequest request, ModelMap model) {
		Tcomment tcomment = tcommentService.get(pid);
		model.addAttribute("tcomment", tcomment);
      model.addAttribute("actionEdit", request.getParameter("actionEdit"));
        setDict(model);
		return "tcommentEdit";
	}

	/**
	 * 民意警务意见采集详情
	 * 
	 * @param pid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcommentDetail.do")
	public String detail(long pid, HttpServletRequest request, ModelMap model) {
		Tcomment tcomment = tcommentService.get(pid);
		model.addAttribute("tcomment", tcomment);
        setDict(model);
		return "tcommentView";
	}

	/**
	 * 民意警务意见采集查询（分页）
	 * 
	 * @param tcomment
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcommentList.do")
	public String listPage(Tcomment tcomment, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tcommentService.getTcommentByPage(toMap(tcomment),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tcommentList";
	}

	/**
	 * 民意警务意见采集查询
	 * 
	 * @param tcomment
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcommentList1.do")
	public String list(Tcomment tcomment, HttpServletRequest request, ModelMap model) {
		List<Tcomment> list = tcommentService.getTcomment(toMap(tcomment));
		model.addAttribute("list", list);
        setDict(model);
		return "tcommentList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tcomment
	 * @return
	 */	private Map<String,Object> toMap(Tcomment tcomment){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tcomment.getPid()!=null&&!tcomment.getPid().equals("")){
			map.put("pid", tcomment.getPid());
		}
		if(tcomment.getUsername()!=null&&!tcomment.getUsername().equals("")){
			map.put("username", tcomment.getUsername());
		}
		if(tcomment.getContent()!=null&&!tcomment.getContent().equals("")){
			map.put("content", tcomment.getContent());
		}
		if(tcomment.getCreatetime()!=null&&!tcomment.getCreatetime().equals("")){
			map.put("createtime", tcomment.getCreatetime());
		}
		if(tcomment.getStatus()!=null&&!tcomment.getStatus().equals("")){
			map.put("status", tcomment.getStatus());
		}
		if(tcomment.getVoice()!=null&&!tcomment.getVoice().equals("")){
			map.put("voice", tcomment.getVoice());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TCOMMENT_STATUS));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tcomment tcomment, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TcommentService tcommentService;   
}